package com.umfwechat.h5.controller.billDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.prop.UmfPropModel;
import com.umfwechat.common.constant.PropertyConsts;
import com.umfwechat.util.DateTimesUtil;
import com.umfwechat.util.HttpUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.StringUtil;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.soopay.PayOrder;
import com.umpay.sp.service.soopay.remote.PayOrderDalServiceRemote;
import com.umpay.sp.service.soopay.remote.PayOrderServiceRemote;
import com.umpay.sp.util.AmountUtil;
import com.umpay.sp.xml.XmlData;

/**
 * 日交易明细
 * 1.微信总计-立马付
 * 2.每日交易列表-立马付
 * 3.订单详情-立马付
 * 4.POS机总计-POS机
 * 5.每日交易列表-POS机
 * 6.POS机交易详情
 * 
 * @author liuchunyan
 * @date 2017-05-22 上午 10:58:18
 */
@Controller
@RequestMapping("/h5/billDetail")
public class DayDealListController {
	private static Logger logger = LoggerFactory.getLogger(DayDealListController.class);

	//订单
	@Resource
	private PayOrderDalServiceRemote payOrderDalServiceRemote;
	
	//订单
	@Resource
	private PayOrderServiceRemote payOrderServiceRemote;
	
	//金额正负
	private String amtIcon = "+";
	
	 /* 微信总计-立马付
	@RequestMapping(value="/getWxTotalAmt",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getWxTotalAmt(HttpServletRequest request, HttpServletResponse response){
		logger.info("【微信总计-立马付】开始"); 
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String storeId = request.getParameter("storeId");
		String orderDate = request.getParameter("orderDate");
		String payType = request.getParameter("payType");
		
		try{
			String[] payList = payType.split(",");
			
			if(StringUtil.isEmpty(storeId)){
				logger.info("店铺编号不能为空！"); 
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "店铺编号不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			if(StringUtil.isEmpty(orderDate)){
				logger.info("交易日期不能为空！"); 
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "交易日期不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//获取产品编号字符串
			String productIdList = getProductIdList(payList);
            
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			reqMessage.put("merId", storeId);
			reqMessage.put("startDate", orderDate);
			reqMessage.put("endDate", orderDate);
			reqMessage.put("orderStateList", "2,4");
			reqMessage.put("productIdList", productIdList);
			//订单查询
			logger.info("统计订单金额和笔数 reqMessage = " + reqMessage);
			ResMessage resMessage = payOrderDalServiceRemote.countPayOrderAmtAndNum(reqMessage);
			logger.info("统计订单金额和笔数 resMessage = " + resMessage);
			if(!RetCode.SUCCESS.equals(resMessage.getRetCode())){
				logger.info("统计订单金额和笔数失败！");
	        	resultMap.put("retCode", RetCode.FAIL);
	    		resultMap.put("retMsg", "统计订单金额和笔数失败！");
	    		return JSONObject.fromObject(resultMap).toString();
			}
			//统计金额(分转元)
			resultMap.put("totalAmt", AmountUtil.Cent2Dollar2(resMessage.getValue("successAmt").toString()));
			//统计笔数
			resultMap.put("totalNum", resMessage.getValue("successNum"));
			logger.info("【微信总计-立马付】返回数据：" + resultMap); 
			return JSONObject.fromObject(resultMap).toString();
			
		} catch (Exception e) {
			logger.info("获取日交易列表失败",e);
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "获取日交易列表失败");
			return JSONObject.fromObject(resultMap).toString();
		}
	}*/
	
	/**
	 * 每日交易列表-立马付
	 * 
	 * @param reqMessage
	 * @return ResMessage
	 * @throws 
	 * 	 */
	@RequestMapping(value="/getDayPayOrderList",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getDayPayOrderList(HttpServletRequest request, HttpServletResponse response){
		logger.info("日交易明细列表查询接口 开始");
		String storeId = request.getParameter("storeId");
		String payType = request.getParameter("payType");
		logger.info("-------"+payType+"-------");
		String orderDateStr = request.getParameter("orderDate");
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
//		String[] payList = payType.split(",");
		JSONArray jsonArray = JSONArray.fromObject(payType);
		List<String> payList = JSONArray.toList(jsonArray);
		
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> resultList = new ArrayList<String>();
		
		if(StringUtil.isEmpty(storeId)){
			logger.info("店铺编号不能为空！"); 
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "店铺编号不能为空！");
			return JSONObject.fromObject(resultMap).toString();
		}
		if(StringUtil.isEmpty(orderDateStr)){
			logger.info("交易日期不能为空！"); 
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "交易日期不能为空！");
			return JSONObject.fromObject(resultMap).toString();
		}
		if(StringUtil.isEmpty(pageSizeStr) 
				|| Integer.parseInt(pageSizeStr) == 0){
			logger.info("每页条数必须大于0！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "每页条数必须大于0！");
			return JSONObject.fromObject(resultMap).toString();
		}
		if(StringUtil.isEmpty(currentPageStr) 
				|| Integer.parseInt(currentPageStr) == 0){
			logger.info("当前页数必须大于0！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "当前页数必须大于0！");
			return JSONObject.fromObject(resultMap).toString();
		}
		
		try{
            //字符串拼接
            StringBuilder productIdSb = new StringBuilder();
            if(payList == null || payList.size() == 0){
            	//立马付-微信
            	productIdSb.append(PropertyConsts.PRODUCTID_WECHAT);
            	//立马付-支付宝
            	productIdSb.append(",").append(PropertyConsts.PRODUCTID_ALIPAY);
            	//微信公众号支付
            	productIdSb.append(",").append(PropertyConsts.PRODUCTID_WECHATPAY);
            }else{
            	for(int i=0;i<payList.size();i++){
            		if(i > 0){
            			productIdSb.append(",");
            		}
            		Integer type = Integer.parseInt(payList.get(i));
                	if(type == 1){//立马付-微信
                		productIdSb.append(PropertyConsts.PRODUCTID_WECHAT);
                    }else if(type == 2){//立马付-支付宝
                    	productIdSb.append(PropertyConsts.PRODUCTID_ALIPAY);
                    }else if(type == 3){//微信公众号支付
                    	productIdSb.append(PropertyConsts.PRODUCTID_WECHATPAY);
                    }
                }
            }
            
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			reqMessage.getReqMap().put("merId", storeId);
			reqMessage.getReqMap().put("startDate", orderDateStr);
			reqMessage.getReqMap().put("endDate", orderDateStr);
			reqMessage.getReqMap().put("currentPage", currentPageStr);
			reqMessage.getReqMap().put("pageSize", pageSizeStr);
			reqMessage.getReqMap().put("orderStateList", "2,4");
			reqMessage.getReqMap().put("productIdList", productIdSb.toString());
			//订单查询
			logger.info("订单查询 reqMessage = " + reqMessage);
			ResMessage resMessage = payOrderDalServiceRemote.queryPayOrderListDesc(reqMessage);
			logger.info("订单查询 resMessage = " + resMessage);
			if(resMessage.getRetCode().equals(RetCode.SUCCESS)){
				List<PayOrder> payOrders = (List<PayOrder>)resMessage.getValue(XmlData.RESULT_LIST);
				for(PayOrder payOrder : payOrders){
					Map<String, Object> dataMap = new HashMap<String, Object>();
					dataMap.put("inTime", DateTimesUtil.timestampToStr(payOrder.getInTime(), DateTimesUtil.formatTime18Str));
					dataMap.put("amountY", payOrder.getAmountY());
					String productId = payOrder.getProductId();
					if(PropertyConsts.PRODUCTID_WECHAT.indexOf(productId) >= 0){//立马付-微信
						dataMap.put("paymentUserName", "微信用户");
						dataMap.put("payType", 1);
					}else if(PropertyConsts.PRODUCTID_ALIPAY.indexOf(productId) >= 0){//立马付-支付宝
						dataMap.put("paymentUserName", "支付宝用户");
						dataMap.put("payType", 2);
					}else if(PropertyConsts.PRODUCTID_WECHATPAY.indexOf(productId) >= 0){//微信公众号支付
						dataMap.put("paymentUserName", "微信用户");
						dataMap.put("payType", 3);
					}else{
						dataMap.put("paymentUserName", "");
						dataMap.put("payType", null);
					}
					dataMap.put("tradeNo", payOrder.getTradeNo());
					resultList.add(JSONObject.fromObject(dataMap).toString());
				}
	        	resultMap.put("retCode", RetCode.SUC);
	    		resultMap.put("retMsg", "获取日交易列表成功！");
	    		resultMap.put("resultList", resultList);
	    		logger.info("获取日交易列表成功！resultMap = " + resultMap);
	    		return JSONObject.fromObject(resultMap).toString();
			}else{
				logger.info("订单查询信息失败！resMessage = " + resMessage);
	        	resultMap.put("retCode", RetCode.FAIL);
	    		resultMap.put("retMsg", "订单查询信息失败！");
	    		resultMap.put("resultList", resultList);
	    		return JSONObject.fromObject(resultMap).toString();
			}
		} catch (Exception e) {
			logger.info("获取日交易列表失败",e);
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "获取日交易列表失败");
			return JSONObject.fromObject(resultMap).toString();
		}
	}
	
	/**
	 * 订单详情-立马付
	 * 
	 * @param reqMessage
	 * @return ResMessage
	 * @throws 
	 * 	 */
	@RequestMapping(value="/getPayOrderInfo",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getPayOrderInfo(HttpServletRequest request, HttpServletResponse response){
		logger.info("订单详情查询接口 开始");
		String tradeNo = request.getParameter(XmlData.TRADENO);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(StringUtil.isEmpty(tradeNo)){
			logger.info("U付订单号不能为空！"); 
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "U付订单号不能为空！");
			return JSONObject.fromObject(resultMap).toString();
		}
		try{
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			reqMessage.getReqMap().put(XmlData.TRADENO, tradeNo);
			//订单详细信息查询（根据U付订单号查询）
			logger.info("订单详细信息查询（根据U付订单号查询）reqMessage = " + reqMessage);
			ResMessage resMessage = payOrderServiceRemote.getPayOrderInfByTradeNo(reqMessage);
			logger.info("订单详细信息查询（根据U付订单号查询）resMessage = " + resMessage);
			if(resMessage.getRetCode().equals(RetCode.SUCCESS)){
				PayOrder payOrder = (PayOrder)resMessage.getValue(XmlData.PAYORDER);
				if(payOrder != null){
					Map<String, Object> dataMap = new HashMap<String,Object>();
					dataMap.put("inTime", DateTimesUtil.timestampToStr(payOrder.getInTime(), DateTimesUtil.formatTime18Str));
					dataMap.put("amountY", payOrder.getAmountY());
					String productId = payOrder.getProductId();
					if(PropertyConsts.PRODUCTID_WECHAT.indexOf(productId) >= 0){//立马付-微信
						dataMap.put("paymentUserName", "微信用户");
						dataMap.put("payType", "1");
					}else if(PropertyConsts.PRODUCTID_ALIPAY.indexOf(productId) >= 0){//立马付-支付宝
						dataMap.put("paymentUserName", "支付宝用户");
						dataMap.put("payType", "2");
					}else if(PropertyConsts.PRODUCTID_WECHATPAY.indexOf(productId) >= 0){//微信公众号支付
						dataMap.put("paymentUserName", "微信用户");
						dataMap.put("payType", "3");
					}else{
						dataMap.put("paymentUserName", "");
						dataMap.put("payType", "");
					}
					dataMap.put("tradeNo", payOrder.getTradeNo());
					logger.info("订单详细信息查询成功！");
		        	resultMap.put("retCode", RetCode.SUC);
		    		resultMap.put("retMsg", "订单详细信息查询成功！");
		    		resultMap.put("payOrder", dataMap);
		    		logger.info("订单详细信息查询 resultMap = " + resultMap);
		    		return JSONObject.fromObject(resultMap).toString();
				}
			}
			logger.info("订单详情查询失败！");
        	resultMap.put("retCode", RetCode.FAIL);
    		resultMap.put("retMsg", "订单详情查询失败！");
    		return JSONObject.fromObject(resultMap).toString();
		} catch (Exception e) {
			logger.info("订单详情查询失败！");
        	resultMap.put("retCode", RetCode.FAIL);
    		resultMap.put("retMsg", "订单详情查询失败！");
    		return JSONObject.fromObject(resultMap).toString();
		}
	}
	
	/* POS机总计-POS机
	@RequestMapping(value="/getPosTotalAmt",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getPosTotalAmt(HttpServletRequest request, HttpServletResponse response){
		logger.info("【POS机总计-POS机】开始"); 
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//店铺编号
		String storeId = request.getParameter("storeId");
		//交易日期
		String orderDate = request.getParameter("orderDate");
		//交易类型
		String tranType = request.getParameter("tranType");
		//银行卡类型
		String cardType = request.getParameter(CommonConsts.CARD_TYPE);
		
		try{
			JSONArray jsonArray = JSONArray.fromObject(tranType);
			List<String> payList = JSONArray.toList(jsonArray);
			
			if(StringUtil.isEmpty(storeId)){
				logger.info("店铺编号不能为空！"); 
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "店铺编号不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			if(StringUtil.isEmpty(orderDate)){
				logger.info("交易日期不能为空！"); 
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "交易日期不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//获取管理员基本信息
			Map<String, String> crmResult = getAgentManager(storeId);
			
			//商户交易统计查询 offline-serv1/0903
			String url = UmfPropModel.INSTANCE.getPropValue("saas.pos.offlineserv10903");
			XStream xstream = new XStream(new DomDriver());
			Map<String,Object> paramMap = new HashMap<String,Object>();
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			paramMap.put(CommonConsts.RPID, reqMessage.getRpid());
			paramMap.put(CommonConsts.REQDATE, reqMessage.getReqDate());
			paramMap.put(CommonConsts.REQTIME, reqMessage.getReqTime());
			//用户名
			paramMap.put(CommonConsts.USER_ID, crmResult.get(CommonConsts.USERNAME));
			//帐号
			paramMap.put(CommonConsts.ASSIST_ID, crmResult.get(CommonConsts.ID));
			//密码
			paramMap.put(CommonConsts.PASS_WORD, crmResult.get(CommonConsts.PASS_WORD));
			//参数数据来源
			paramMap.put(CommonConsts.DATASOURCE, crmResult.get(CommonConsts.SAAS.toString().toUpperCase()));
			//代理商编号
			paramMap.put(CommonConsts.AGENTID, crmResult.get(CommonConsts.AGENTID));
			//商户号
			paramMap.put(CommonConsts.MERCHANTID, storeId);
			//交易日期
			paramMap.put(CommonConsts.LOCALSYSDATE, orderDate);
			//交易类型
			paramMap.put(CommonConsts.TRAN_TYPE, tranType);
			//卡种
			paramMap.put(CommonConsts.CARD_TYPE, cardType);
			
			//调用CRM
			logger.info("【商户交易统计查询】  路径：" + url + " 入参：" + paramMap);
			Map<String, Object> countMap = (Map<String, Object>) HttpUtil.getResObjPost(url, xstream, paramMap);
			logger.info("【商户交易统计查询】 出参：" + crmResult);
			if(!RetCode.SUCCESS.equals(countMap.get(CommonConsts.RETCODE))){
				logger.info("【商户交易统计查询失败】");
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "商户交易统计查询失败！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//统计金额(分转元)
			resultMap.put("totalAmt", AmountUtil.Cent2Dollar2(countMap.get("tranAmts").toString()));
			//统计笔数
			resultMap.put("totalNum", countMap.get("tranCount"));
			logger.info("【POS机总计-POS机】返回数据：" + resultMap); 
			return JSONObject.fromObject(resultMap).toString();
		} catch (Exception e) {
			logger.info("获取POS机总计-POS机失败",e);
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "获取POS机总计-POS机失败");
			return JSONObject.fromObject(resultMap).toString();
		}
	}*/
	
	/**
	 * 每日交易列表-POS机
	 * 
	 * @param reqMessage
	 * @return ResMessage
	 * @throws 
	 * 	 */
	@RequestMapping(value="/getPosDayPayOrderList",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getPosDayPayOrderList(HttpServletRequest request, HttpServletResponse response){
		logger.info("【每日交易列表-POS机】开始"); 
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//店铺编号
		String storeId = request.getParameter(CommonConsts.STOREID);
		//交易日期
		String orderDate = request.getParameter(CommonConsts.ORDERDATE);
		//交易类型
		String tranType = request.getParameter(CommonConsts.TRAN_TYPE);
		//银行卡类型
		String cardType = request.getParameter(CommonConsts.CARD_TYPE);
		//当前页
		String currentPage = request.getParameter(CommonConsts.CURRENT_PAGE);
		//每页条数
		String pageSize = request.getParameter(CommonConsts.PAGE_SIZE);
		
		try{
			if(StringUtil.isEmpty(storeId)){
				logger.info("店铺编号不能为空！"); 
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "店铺编号不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			if(StringUtil.isEmpty(orderDate)){
				logger.info("交易日期不能为空！"); 
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "交易日期不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//商户交易查询接口 /offline-serv1/0901
			String url = UmfPropModel.INSTANCE.getPropValue("saas.pos.offlineserv10901");
			Map<String,Object> paramMap = new HashMap<String,Object>();
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			paramMap.put(CommonConsts.RPID, reqMessage.getRpid());
			paramMap.put(CommonConsts.REQDATE, reqMessage.getReqDate());
			paramMap.put(CommonConsts.REQTIME, reqMessage.getReqTime());
			//参数数据来源
			paramMap.put(CommonConsts.DATASOURCE, CommonConsts.SAAS);
			//当前页
			paramMap.put(CommonConsts.PAGE_NUM, currentPage);
			//每页显示记录数
			paramMap.put(CommonConsts.PAGE_SIZE, pageSize);
			//商户号
			paramMap.put(CommonConsts.MERCHANTID, storeId);
			//交易日期
			paramMap.put(CommonConsts.LOCALSYSDATE, orderDate);
			//交易类型(3301微信、3501支付宝)
			paramMap.put(CommonConsts.TRAN_TYPE, tranType);
			//卡种(00：借记卡 02:准贷记卡)
			paramMap.put(CommonConsts.CARD_TYPE, cardType);
			
			logger.info("【商户交易查询】  路径：" + url + " 入参：" + paramMap);
			Map<String, Object> posMap = HttpUtil.httpPostForm2Xml(url, paramMap);
			logger.info("【商户交易查询】 出参：" + posMap);
			if(posMap == null || !RetCode.SUCCESS.equals(posMap.get(CommonConsts.RETCODE))){
				logger.info("【商户交易查询失败】");
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "商户交易查询失败！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
			Object listStr = posMap.get("listStr");
			if(listStr != null && StringUtil.isNotEmpty(listStr.toString())){
				List<Map<String, String>> posMapList = new Gson().fromJson(listStr.toString(), 
						new TypeToken<List<Map<String, String>>>(){}.getType());
				for(Map<String, String> map : posMapList){
					//获取交易类型名称
					String tranTypeName = getTranTypeName(map.get(CommonConsts.TRAN_TYPE), map.get(CommonConsts.PAY_TYPE));
					//获取卡类型名称
					String cardTypeName = getCardTypeName(map.get(CommonConsts.CARD_TYPE));
					
					//交易时间	
					String orderTime = "";
					if(StringUtil.isNotEmpty(map.get(CommonConsts.LOCALSYSDATE))
							&& StringUtil.isNotEmpty(map.get(CommonConsts.LOCALSYSTIME))){
						orderTime = DateTimesUtil.date18(map.get(CommonConsts.LOCALSYSDATE), map.get(CommonConsts.LOCALSYSTIME));
					}
					
					Map<String, String> dataMap = new HashMap<String, String>();
					//交易流水	
					dataMap.put(XmlData.HOSTLSNO, map.get(XmlData.HOSTLSNO));
					//卡号	
					dataMap.put(XmlData.CARDNO, map.get(XmlData.CARDNO));
					//卡类型	
					dataMap.put(CommonConsts.CARD_TYPE, cardTypeName);
					//交易时间	
					dataMap.put(CommonConsts.ORDER_TIME, orderTime);
					//交易金额(分转元)
					dataMap.put(CommonConsts.TRAN_AMT, amtIcon + AmountUtil.Cent2Dollar2(map.get(CommonConsts.TRAN_AMT)));
					//交易类型	
					dataMap.put(CommonConsts.TRAN_TYPE, tranTypeName);
					resultList.add(dataMap);
				}
			}
			resultMap.put(CommonConsts.RETCODE, RetCode.SUCCESS);
			resultMap.put(CommonConsts.RETMSG, "商户交易查询成功！");
			resultMap.put("resultList", resultList);
			logger.info("【每日交易列表-POS机】返回数据：" + resultMap); 
			return JSONObject.fromObject(resultMap).toString();
		} catch (Exception e) {
			logger.info("获取每日交易列表-POS机失败",e);
			resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
			resultMap.put(CommonConsts.RETMSG, "获取每日交易列表-POS机失败");
			return JSONObject.fromObject(resultMap).toString();
		}
	}
	
	/**
	 * POS机交易详情
	 * 
	 * @param reqMessage
	 * @return ResMessage
	 * @throws 
	 * 	 */
	@RequestMapping(value="/getPosPayOrder",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getPosPayOrder(HttpServletRequest request, HttpServletResponse response){
		logger.info("【POS机交易详情】 开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//交易流水
		String hostLsNo = request.getParameter(XmlData.HOSTLSNO);
		//店铺编号
		String storeId = request.getParameter(XmlData.STORE_ID);
		//交易日期
		String orderDate = request.getParameter(XmlData.ORDERDATE);
		try{
			if(StringUtil.isEmpty(hostLsNo)){
				logger.info("交易流水不能为空！"); 
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "交易流水不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
 
			if(StringUtil.isEmpty(storeId)){
				logger.info("店铺编号不能为空！"); 
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "店铺编号不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			if(StringUtil.isEmpty(orderDate)){
				logger.info("交易日期不能为空！"); 
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "交易日期不能为空！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//商户详情查询接口 /offline-serv1/0902
			String url = UmfPropModel.INSTANCE.getPropValue("saas.pos.offlineserv10902");
			Map<String,Object> paramMap = new HashMap<String,Object>();
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			paramMap.put(CommonConsts.RPID, reqMessage.getRpid());
			paramMap.put(CommonConsts.REQDATE, reqMessage.getReqDate());
			paramMap.put(CommonConsts.REQTIME, reqMessage.getReqTime());
			//参数数据来源
			paramMap.put(CommonConsts.DATASOURCE, CommonConsts.SAAS);
			//本地交易流水
			paramMap.put(XmlData.HOSTLSNO, hostLsNo);
			//商户号
			paramMap.put(CommonConsts.MERCHANTID, storeId);
			//交易日期
			paramMap.put(CommonConsts.LOCALSYSDATE, orderDate);
			
			logger.info("【商户详情查询】  路径：" + url + " 入参：" + paramMap);
			Map<String, Object> posMap = HttpUtil.httpPostForm2Xml(url, paramMap);
			logger.info("【商户详情查询】 出参：" + posMap);
			if(!RetCode.SUCCESS.equals(posMap.get(CommonConsts.RETCODE))){
				logger.info("【商户详情查询失败】");
				resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
				resultMap.put(CommonConsts.RETMSG, "商户详情查询失败！");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//获取交易类型名称
			String tranTypeName = getTranTypeName(StringUtil.trim(posMap.get(CommonConsts.TRAN_TYPE))
					,StringUtil.trim(posMap.get(CommonConsts.PAY_TYPE)));
			//获取交易状态名称
			String tranStateName = getTranStateName(StringUtil.trim(posMap.get(CommonConsts.SAF_FLAG))
					,StringUtil.trim(posMap.get(CommonConsts.SAF_FLAG)));
			//获取卡类型名称
			String cardTypeName = getCardTypeName(StringUtil.trim(posMap.get(CommonConsts.CARD_TYPE)));
			//交易时间	
			String orderTime = "";
			String localSysDate = StringUtil.trim(posMap.get(CommonConsts.LOCALSYSDATE));
			String localSysTime = StringUtil.trim(posMap.get(CommonConsts.LOCALSYSTIME));
			if(StringUtil.isNotEmpty(localSysDate)
					&& StringUtil.isNotEmpty(localSysTime)){
				orderTime = DateTimesUtil.date18(localSysDate, localSysTime);
			}
			
			//退费申请日期
			String refundDate = StringUtil.trim(posMap.get(CommonConsts.REFUND_DATE));
			if(StringUtil.isNotEmpty(refundDate)){
				refundDate = DateTimesUtil.date10(refundDate);
			}
			
			//交易金额(分转元)
			resultMap.put(CommonConsts.TRAN_AMT, amtIcon + AmountUtil.Cent2Dollar2(StringUtil.trim(posMap.get(CommonConsts.TRAN_AMT))));
			//交易时间
			resultMap.put(CommonConsts.ORDER_TIME, orderTime);
			//流水
			resultMap.put(XmlData.HOSTLSNO, StringUtil.trim(posMap.get(XmlData.HOSTLSNO)));
			//终端号
			resultMap.put(CommonConsts.TERMINAL_ID, StringUtil.trim(posMap.get(CommonConsts.TERMINAL_ID)));
			//外部商户号
			resultMap.put(CommonConsts.WMERCHANT_ID, StringUtil.trim(posMap.get(CommonConsts.WMERCHANT_ID)));
			//商户订单号
			resultMap.put(CommonConsts.ORDER_NO, StringUtil.trim(posMap.get(CommonConsts.ORDER_NO)));
			//卡号
			resultMap.put(XmlData.CARDNO, StringUtil.trim(posMap.get(XmlData.CARDNO)));
			//卡种
			resultMap.put(CommonConsts.CARD_TYPE, cardTypeName);
			//参考号
			resultMap.put(CommonConsts.TRAN_RRN, StringUtil.trim(posMap.get(CommonConsts.TRAN_RRN)));
			//交易类型
			resultMap.put(CommonConsts.TRAN_TYPE, tranTypeName);
			//退费申请日期	
			resultMap.put(CommonConsts.REFUND_DATE, refundDate);
			//交易状态
			resultMap.put(CommonConsts.TRAN_STATE, tranStateName);
			resultMap.put(CommonConsts.RETCODE, RetCode.SUCCESS);
			resultMap.put(CommonConsts.RETMSG, "商户详情查询成功！");
			logger.info("【POS机交易详情】返回数据：" + resultMap); 
			return JSONObject.fromObject(resultMap).toString();
		} catch (Exception e) {
			logger.info("获取POS机交易详情失败",e);
			resultMap.put(CommonConsts.RETCODE, RetCode.FAIL);
			resultMap.put(CommonConsts.RETMSG, "获取POS机交易详情失败");
			return JSONObject.fromObject(resultMap).toString();
		}
	}
	
	/**
	 * 支付类型名称
	 * 
	 * @param tranType 交易类型
	 * @param payType POS增值业务类型
	 * @return
	 */
	private String getTranTypeName(String tranType, String payType){
		//金额正负
		amtIcon = "+";
		String tranTypeName = "";
		if("1".equals(tranType)){
			if("3301".equals(payType) || "3305".equals(payType)){
				tranTypeName = "微信支付";
			}else if("3501".equals(payType) || "3505".equals(payType)){
				tranTypeName = "支付宝支付";
			}else{
				tranTypeName = "银行卡消费";
			}
		}else if("4".equals(tranType) || "5".equals(tranType)){
			tranTypeName = "预授权确认";
		}else if("9".equals(tranType)){
			tranTypeName = "退货";
			amtIcon = "-";
		}else if("12".equals(tranType)){
			tranTypeName = "撤销";
			amtIcon = "-";
		}else if("15".equals(tranType)){
			tranTypeName = "预授权通知";
		}else if("122".equals(tranType)){
			tranTypeName = "预授权申请撤销";
			amtIcon = "-";
		}else if("123".equals(tranType)){
			tranTypeName = "预授权完成撤销";
			amtIcon = "-";
		}
		return tranTypeName;
	}
	
	/**
	 * 支付状态名称
	 * 
	 * @param safFlag 冲正标识
	 * @param tranFlag 交易标识
	 * @return
	 */
	private String getTranStateName(String safFlag, String tranFlag){
		String tranStateName = "";
		if("1".equals(safFlag)){
			tranStateName = "已冲正";
		}else if("0".equals(tranFlag)){
			tranStateName = "正常";
		}else if("1".equals(tranFlag)){
			tranStateName = "已撤消";
		}else if("2".equals(tranFlag)){
			tranStateName = "已确认";
		}else if("4".equals(tranFlag)){
			tranStateName = "已退货";
		}else if("5".equals(tranFlag)){
			tranStateName = "未明";
		}
		return tranStateName;
	}
	
	/**
	 * 卡类型名称
	 * 
	 * @param cardType 卡类型
	 * @return
	 */
	private String getCardTypeName(String cardType){
		String cardTypeName = "";
		if("00".equals(cardType)){
			cardTypeName = "借记卡";
		}else if("01".equals(cardType)){
			cardTypeName = "贷记卡";
		}
		return cardTypeName;
	}
}